package com.alexeyhanin.tapestry.goodies.util.select;

import static com.alexeyhanin.tapestry.goodies.util.StringUtil.notNullString;

/**
 * @author aha
 * @since 2009.28.11
 *        $Id: FormattingPropertyAdapter.java 15 2009-11-30 17:52:21Z gagarin61 $
 */
public class FormattingPropertyAdapter implements PropertyAdapter<String> {

    private String format;
    private org.apache.tapestry5.ioc.services.PropertyAdapter[] propertyAdapters;

    public FormattingPropertyAdapter(String format, org.apache.tapestry5.ioc.services.PropertyAdapter[] propertyAdapters) {
        this.format = format;
        this.propertyAdapters = propertyAdapters;
    }

    public String getFormat() {
        return format;
    }

    public org.apache.tapestry5.ioc.services.PropertyAdapter[] getPropertyAdapters() {
        return propertyAdapters;
    }

    public String get(Object object) {
        final String[] values = new String[propertyAdapters.length];
        for (int i = 0; i < propertyAdapters.length; i++) {
            final Object value = propertyAdapters[i].get(object);
            final String stringValue = notNullString(value);
            values[i] = stringValue;
        }
        return String.format(format, values);
    }

}
